package com.meta.user.service.impl;


import com.meta.cache.NdCacheService;
import com.meta.common.CommonInfo;
import com.meta.common.ConstDef;
import com.meta.nd.filter.bo.FilterParamDef;
import com.meta.nd.filter.utils.FilterDefBuilder;
import com.meta.nd.filter.vo.FilterResultVo;
import com.meta.nd.service.NvFilterService;
import com.meta.nd.service.NvService;
import com.meta.nd.vo.Nd;
import com.meta.nd.vo.Nv;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class UserServiceImpl implements UserService{

    @Resource
    NvService nvService;
    @Resource
    NdCacheService ndCacheService;
    @Resource
    NvFilterService filterService;

@Override
public boolean verifyUser(String userCode, String password){
    try {
        Nv nv = this.getUser(userCode);
        if (nv == null)
            return false;
        String realPwd = nv.getRawStringByName("password");
        if (!password.equals(realPwd))
            return false;
    }catch ( Exception e ){
        log.error(e.getMessage(),e);
        return false;
    }
    return true;
}

public Nv getUser(String userCode){
    Nd nd = ndCacheService.getNd(ConstDef.ND_CODE_USER);
    FilterDefBuilder builder = FilterDefBuilder.createBuilder(nd,nd.getNdCode(),nd.getNdCode(),nd.getNdCode());
    builder.addFilterFd(nd.getNdName(),"userCode",null,ConstDef.LOGIC_OPERATION_EQUAL,userCode);
    builder.addResultFd(nd.getNdName(),"userCode",null);
    builder.addResultFd(nd.getNdName(),"password",null);
    builder.addResultFd(nd.getNdName(),"id",null);
    FilterParamDef filterDef = builder.getFilterParamDef();
    FilterResultVo filterResult = filterService.filterNvsByParamDef(CommonInfo.getDefault(),filterDef);
    if ( filterResult.getTotal() == 0 ){
        return null;
    }
    if (filterResult.getTotal() > 1 ){
        throw new RuntimeException(String.format("用户代码为:%s存在多条记录", userCode));
    }
    long id = filterResult.getNvs().get(0).getId();
    return this.nvService.getNv(CommonInfo.getDefault(),nd.getNdCode(),id);
}

    @Override
    public boolean bindDevice(String deviceId, String userCode,String password){
        if (!this.verifyUser(userCode,password)){
            throw new RuntimeException("用户密码不正确!");
        }
        Nv userNv = this.getUser(userCode);
        userNv.setFvByName("deviceId", deviceId);
        userNv.setOperationFlag(ConstDef.OPERATION_FLAG_UPDATE);
        this.nvService.saveNv(CommonInfo.getDefault(),userNv);
        return true;
    }

    @Override
    public Nv getUserByDeiceId(String deviceId){
        Nd nd = ndCacheService.getNd(ConstDef.ND_CODE_USER);
        FilterDefBuilder builder = FilterDefBuilder.createBuilder(nd,nd.getNdCode(),nd.getNdCode(),nd.getNdCode());
        builder.addFilterFd(nd.getNdName(),"deviceId",null,ConstDef.LOGIC_OPERATION_EQUAL,deviceId);
        builder.addResultFd(nd.getNdName(),"id",null);
        FilterParamDef filterDef = builder.getFilterParamDef();
        FilterResultVo filterResult = filterService.filterNvsByParamDef(CommonInfo.getDefault(),filterDef);
        if ( filterResult.getTotal() == 0 ){
            throw new RuntimeException(String.format("设备没有绑定用户！设备Id:%s", deviceId));
        }
        if (filterResult.getTotal() > 1 ){
            throw new RuntimeException(String.format("设备没有绑定多个用户！设备Id:%s", deviceId));
        }
        long id = filterResult.getNvs().get(0).getId();
        return this.nvService.getNv(CommonInfo.getDefault(),nd.getNdCode(),id);
    }

}
